package br.com.beyondclick.view;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

import br.com.beyondclick.model.entity.Cheque;
import br.com.beyondclick.model.repository.ChequeRepository;
/**
 * @jsp.tag name="listarCheques" body-content="empty"
 */
public class ListaChequeTag extends TagSupport {
	private static final long serialVersionUID = 1L;

	private final static Logger logger = Logger.getLogger(ListaChequeTag.class);

	@EJB(beanName="ChequeRepositoryBeanRemote")
	private ChequeRepository chequeRepository;
	
	@Override
	public int doStartTag() throws JspException {
		try{
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();

			List<Cheque> cheques = chequeRepository.findComSaldoDisponivel();
			Comparator<Cheque> cCheque = new Comparator<Cheque>(){
				public int compare(Cheque c1, Cheque c2) {
					return Long.valueOf(c1.getNumero()).compareTo(c2.getNumero());
				}
			};

			Collections.sort(cheques, cCheque);

			request.setAttribute("cheques", cheques);
		}catch(Exception e){
			logger.warn("Erro ao recuperar a lista de cheques.", e);
		}
		
		return SKIP_BODY;
	}
}
